package com.hpf.lease.web.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hpf.lease.common.result.Result;
import com.hpf.lease.model.bean.SystemUser;
import com.hpf.lease.web.admin.dto.SystemUserSearchSubmitDto;
import com.hpf.lease.web.admin.service.SystemUserService;
import com.hpf.lease.web.admin.vo.SystemUserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/system/user")
@Tag(name = "系统用户管理")
public class SystemUserController {
    @Resource
    SystemUserService service;
    @PostMapping("/updateStatusByUserId")
    @Operation(summary = "根据ID修改后台用户状态")
    public Result<Object> updateStatusByUserId(@RequestParam long id,@RequestParam Integer status){
        return Result.build(service.update(Wrappers.lambdaUpdate(SystemUser.class)
                .eq(SystemUser::getId , id)
                .set(SystemUser::getStatus , status)));
    }
    @PostMapping("/saveOrUpdate")
    @Operation(summary = "保存或更新后台用户信息")
    public Result<Object> saveOrUpdate(@RequestBody SystemUser systemUser){
        return Result.build(service.saveOrUpdateInfo(systemUser));
    }
    @GetMapping("/page")
    @Operation(summary = "根据条件分页查询后台用户列表")
    public Result<Page<SystemUserInfoVo>> getPage(SystemUserSearchSubmitDto submitDto){
        return Result.ok(service.getPageItem(submitDto));
    }
    @GetMapping("/isUserNameAvailable")
    @Operation(summary = "判断后台用户名是否可用")
    public Result<Object> isUserNameAvailable(@RequestParam String username , Long id){
        return Result.build(service.getOne(Wrappers.lambdaQuery(SystemUser.class)
                .eq(SystemUser::getUsername , username)
                .eq(id != null , SystemUser::getId , id)).getStatus() == 0);
    }
    @GetMapping("/getById")
    @Operation(summary = "根据id查询系统用户")
    public Result<SystemUserInfoVo> getById(@RequestParam long id){
        return Result.ok(service.getDetailById(id));
    }
    @DeleteMapping("/deleteById")
    @Operation(summary = "根据ID删除后台用户信息")
    public Result<Object> deleteById(@RequestParam long id){
        return Result.build(service.removeById(id));
    }
}
